﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Faster.Entity;
using System.Xml;

namespace Faster.GameBLL
{
    public class PlantHelper
    {
        public UserInfo User { get; set; }

        public PlantHelper()
        {

        }

        public void GetAllPlant()
        {
            string url = "http://s1.kaixin001pvz.youkia.com/pvz/index.php/Warehouse/index/sig/08dd69957d7b01d7082927c6e4401d28?1322639768944";

            string xml = RequestHelper.GetRequest(url, this.User.Cookies);

            LoadPlant(xml);
        }

        private void LoadPlant(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            XmlNodeList list = doc.SelectNodes("/root/organisms/item");

            if (this.User.Plants == null)
            {
                this.User.Plants = new List<PlantInfo>();
            }
            foreach (XmlNode node in list)
            {
                PlantInfo plant = new PlantInfo();
                for (int i = 0; i < node.Attributes.Count; i++)
                {
                    if (node.Attributes[i].Name == "id")
                    {
                        plant.PlantID = Convert.ToInt32(node.Attributes[i].Value);
                    }
                    else if (node.Attributes[i].Name == "at")
                    {
                        plant.PlantAttack = Convert.ToInt32(node.Attributes[i].Value);
                    }
                    else if (node.Attributes[i].Name == "mi")
                    {
                        plant.PlantHit = Convert.ToInt32(node.Attributes[i].Value);
                    }
                    else if (node.Attributes[i].Name == "sp")
                    {
                        plant.PlantSpeed = Convert.ToInt32(node.Attributes[i].Value);
                    }
                    else if (node.Attributes[i].Name == "pid")
                    {
                        plant.PlantType = Convert.ToInt32(node.Attributes[i].Value);
                    }
                    else if (node.Attributes[i].Name == "gr")
                    {
                        plant.PlantLevel = Convert.ToInt32(node.Attributes[i].Value);
                    }
                }
                this.User.Plants.Add(plant);
            }
        }
    }
}
